<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" v-on:click="logout()">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" v-on:click="toggleCollapse()">|||</div>
        <el-aside width=" 200px">
          <!-- 侧边栏菜单区域 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#4091ff"
            unique-opened
            v-bind:collapse="isCollapse"
            v-bind:collapse-transition="false"
            router
            v-bind:default-active="activePath"
          >
            <!-- 一级菜单 -->
            <el-submenu
              v-bind:index="item.id + ''"
              v-for="item in menulist"
              v-bind:key="item.id"
            >
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i v-bind:class="iocnsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                v-bind:index="'/' + subItem.path"
                v-for="subItem in item.children"
                v-bind:key="subItem.id"
                v-on:click="saveNavState('/' + subItem.path)"
              >
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import hub from '../utils/hub'
export default {
  data: function() {
    return {
      menulist: [],
      iocnsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },

      isCollapse: false,
      activePath: false
    };
  },
  created: function() {
    this.getMenuList(),
      (this.activePath = window.sessionStorage.getItem("activePath"));
    return hub.$on("saveNavState", this.saveNavState);
  },
  methods: {
    logout: function() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    beforeDestroy: function() {
      return hub.$off("saveNavState", this.saveNavState);
    },
    getMenuList: async function() {
      const { data: res } = await this.$http.get("menus");
      //   console.log(res, 233);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }

      this.menulist = res.data;
      // console.log(res);
    },
    toggleCollapse: function() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState: function(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .iconfont {
    margin-right: 10px;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: 0px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
